<?php

namespace app\controller\admin\material;

use app\model\admin\basic\BasicContent;
use app\model\admin\material\MaterialGame;
use app\model\admin\material\MaterialSpecial;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class Career
 * @package app\controller\admin\ah
 */
class Special extends Backend
{
    protected $model;
    protected $noNeedAuth = ['index', 'info'];
    public function initialize()
    {
        $this->model = new MaterialSpecial();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 列表
     *  all_data参数的值为true时，表示查询表中所有数据集，否则进行分页查询
     * @return mixed
     */
    public function index()
    {
        $where = $this->buildSearchParams();
        $order = $this->buildOrder();
        $allData = $this->request->param('all_data');
        if ($allData) {
            $data = $this->model->where($where)->order($order)->cache(true)->select()->toArray();
        } else {
            $limit = $this->request->param('limit', 10);
            $searchParam = $this->request->param('search_param');
            if(isset($searchParam['special_name'])&&!empty($searchParam['special_name']['value'])){
                $basicGameId = (new MaterialGame())->where('id',$searchParam['material_game_id']['value'])->value("basic_game_id");
                $ids = (new BasicContent())->where('basic_game_id',$basicGameId)->where("status",1)->where("name",'like',"%".$searchParam['special_name']['value']."%")->column("id");
                $where[]=[
                  'material_id','in',$ids
                ];
            }

            $data = $this->model->with(["content","style"])->where($where)->order($order)->paginate($limit)->toArray();
            foreach ($data['data'] as &$item) {
                if (isset($item['url']) && !empty($item['url'])) {
                    $item['url'] = imgUrlToAbsolute($item['url']);
                }
                if (isset($item['icon']) && !empty($item['icon'])) {
                    $item['icon'] = imgUrlToAbsolute($item['icon']);
                }
            }
        }
        return $this->success('数据获取成功', $data);
    }

    //添加
    public function add()
    {
        if(!$this->request->param("game_id")){
            return $this->error("缺少游戏id");
        }
        $gameId = $this->request->param("game_id");
        $post = CommonFun::filterPostData($this->request->post());
        try{
            $post['material_game_id'] = $gameId;
            if ($this->model->create($post)) {
                $this->clearCache();
                return $this->success('添加成功', $post);
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    public function info()
    {
        $id   = $this->request->param('id');
        $info = $this->model->find($id);
        if(is_object($info))$info = $info->toArray();
        $info['game'] = Db::name("material_game")->where("id", $info['material_game_id'])->find();
        return $this->success('获取成功', $info);
    }
}